# coding=utf-8
"""
作者：董新强 
创建时间：2020-3-17
描述：试题选择
"""

import numpy

from enums.enum_params_pos import EnumParamsPos
from services.cat_service.question_bank import QuestionBank


class Selector:
    @staticmethod
    def nearest(array: list, value) -> numpy.ndarray:
        """返回信息-theta绝对值按从小到大排里的索引"""
        array = numpy.asarray(array)
        return numpy.abs(array - value).argsort()

    @staticmethod
    def select_item_use_max_info(aid, init_theta, pre_theta_ls, select_qid_ls, max_exposure_rate=1):
        """
        挑选试题,返回被选中题目的位置
        :param init_theta: 初始值的theta
        :param pre_theta_ls: 之前估计出来theta的列表
        :return: 被挑选的题目的位置
        """
        est_theta = pre_theta_ls[-1] if pre_theta_ls else init_theta  # 选择最新的一个theta

        # 对试题进行进行排序(包括还没做过的)
        ordered_item_ls = QuestionBank.get_item_nearest(aid, idx=EnumParamsPos.info.value, theta=est_theta)

        # 挑选出还没做个的题
        for qid, _ in ordered_item_ls:
            if qid not in select_qid_ls:
                # 可能还需要过滤一下曝光度 todo
                return qid
        return None  # 说明没题目可选了

    @staticmethod
    def select_item_use_difficult(aid, init_theta, pre_theta_ls, select_qid_ls) -> int:
        """根据难度相近来挑选题目"""
        est_theta = pre_theta_ls[-1] if pre_theta_ls else init_theta  # 选择最新的一个theta

        ordered_item_ls = QuestionBank.get_item_nearest(aid, idx=EnumParamsPos.b.value, theta=est_theta)
        for qid, _ in ordered_item_ls:
            if qid not in select_qid_ls:
                return qid

        return 0
